// Excel表格创建实例
var xlsx = require("node-xlsx");
var dateformat = require("dateformat");
var fs = require("fs");
var request = require("request");
var co = require("gq-core/node_modules/co");
// console.log(dateformat(new Date(), "yyyymmddHHMMss"));
var ExcelBuild = {
    build_excel_file: co.wrap(function*(data, type) {
        var excel_models = {
            "user-create-user-with-membertype": {
                head: ["会员号", "开户人姓名", "开户人手机号", "开户人车主类型", "新会员号", "新会员姓名", "新会员手机号", "新会员车主类型", "新会员股份", "新会员积金", "公司进账", "返利链统计"]
            },
            "user-recharge": {
                head: ["会员号", "用户名", "手机号", "车主类型", "充值金额", "手续费"]
            },
            "transfers-to-user": {
                head: ["转账人姓名", "转账人手机号", "转账人车主类型", "收款人姓名", "收款人手机号", "收款人车主类型", "转账额度", "手续费"]
            },
            "confirm-withdrawals-arrive": {
                head: ["会员号", "用户名", "手机号", "车主类型", "提现金额", "手续费"]
            },
            "user-upgrade-asset-level": {
                head: ["会员号", "用户名", "手机号", "升级前车主类型", "升级后车主类型", "支出金额", "手续费"]
            }
        };
        var json = JSON.parse(data);
        var myDatas = [];
        console.log("type,", type);
        myDatas[0] = excel_models[type].head;
        json = json.filter(function(json_item) {
            return json_item.type === type;
        })[0];
        if (json) {
            if (type == "user-create-user-with-membertype") {
                var level_a = 0;
                var level_b = 0;
                var level_c = 0;
                var total_rebates = 0;
                var total_stock = 0;
                var total_assist = 0;
                var total_income = 0;
                var rebate_assist = 0;
                var rebate_value = 0;
                // console.log("hehe")
                function _(cb) {
                    request.get("http://127.0.0.1:4100/admin/statistics/_get_rebates_chain_logs/" + json.list[0].log_id + "/" + json.list[json.list.length - 1].log_id).on("response", function(response) {
                        var bufs = []
                        response.on("data", function(result) {
                            bufs.push(result)
                        });
                        response.on("end", function() {
                            var res = Buffer.concat(bufs);
                            try {
                                res = JSON.parse(res.toString());
                            } catch (e) {
                                cb(e)
                            }
                            res.info.forEach(function(item, index) {
                                rebate_assist += item.data.amounts.filter(function(a) {
                                    return a.key == "cache_assist";
                                })[0].amount;
                                rebate_value += item.data.amounts.filter(function(a) {
                                    return a.key == "cache_balance";
                                })[0].amount;
                            });
                            cb();
                        });
                    });
                };
                yield _;
                total_assist = rebate_assist;
                json.list.forEach(function(column, index) {
                    total_assist = total_assist + 10000;
                    var rebates_chain = 0;
                    total_rebates = total_rebates + rebates_chain;
                    if (column.new_user_asset.car_flag == "天使车主") {
                        level_a = level_a + 1;
                    };
                    if (column.new_user_asset.car_flag == "财富车主") {
                        level_b = level_b + 1;
                    };
                    if (column.new_user_asset.car_flag == "豪金车主") {
                        level_c = level_c + 1;
                    };
                    total_income = total_income + column.amount;
                    total_stock = total_stock + column.new_user_asset.stock;
                    if (!column.owner) {
                        myDatas.push(["管理员开通", "管理员开通", "管理员开通", column.new_user.register_id,
                            column.new_user.real_name,
                            column.new_user.phone_number,
                            column.new_user_asset.car_flag,
                            column.new_user_asset.stock,
                            column.new_user_asset.assist,
                            column.amount,
                            rebates_chain
                        ]);
                    } else {
                        myDatas.push([column.owner.register_id,
                            column.owner.real_name,
                            column.owner.phone_number,
                            column.asset.car_flag,
                            column.new_user.register_id,
                            column.new_user.real_name,
                            column.new_user.phone_number,
                            column.new_user_asset.car_flag,
                            column.new_user_asset.stock,
                            column.new_user_asset.assist,
                            column.amount,
                            rebates_chain
                        ]);
                    }
                });
                myDatas.push(["新建用户数：" + json.list.length, "天使车主数：" + level_a, "财富车主数：" + level_b, "豪金车主数：" + level_c, "新增会员股份：" + total_stock, "新增会员积金：" + total_assist, "总收账：" + total_income, "返利链支出：" + (rebate_value + rebate_assist)]);
            };
            if (type == "user-recharge") {
                var total_amount = 0;
                json.list.forEach(function(column, index) {
                    total_amount = column.amount + total_amount;
                    myDatas.push([
                        column.owner.register_id,
                        column.owner.real_name,
                        column.owner.phone_number,
                        column.asset.car_flag,
                        column.amount,
                        column.fee
                    ]);
                });
                myDatas.push(["充值总笔数：" + json.list.length, "充值总额：" + total_amount]);
            };
            if (type == "transfers-to-user") {
                var total_amount = 0;
                var total_fee = 0;
                json.list.forEach(function(column, index) {
                    total_amount = total_amount + column.amount;
                    total_fee = total_fee + column.fee;
                    myDatas.push([
                        column.owner.real_name,
                        column.owner.phone_number,
                        column.asset.car_flag,
                        column.payee.real_name,
                        column.payee.phone_number,
                        column.payee_asset.car_flag,
                        column.amount,
                        column.fee
                    ]);
                });
                myDatas.push(["转账总笔数：" + json.list.length, "转账金额统计：" + total_amount, "手续费统计：" + total_fee]);
            };
            if (type == "confirm-withdrawals-arrive") {
                var total_amount = 0;
                var total_fee = 0;
                json.list.forEach(function(column, index) {
                    total_amount = total_amount + column.amount;
                    total_fee = total_fee + column.fee;
                    myDatas.push([
                        column.owner.register_id,
                        column.owner.real_name,
                        column.owner.phone_number,
                        column.asset.car_flag,
                        column.amount,
                        column.fee
                    ]);
                });
                myDatas.push(["提现总笔数：" + json.list.length, "提现金额统计：" + total_amount, "手续费统计：" + total_fee]);
            };
            if (type == "user-upgrade-asset-level") {
                var level_to_b = 0;
                var level_to_c = 0;
                json.list.forEach(function(column, index) {
                    if (new_data.level == 2) {
                        level_to_b = level_to_b + 1;
                    };
                    if (new_data.level == 3) {
                        level_to_c = level_to_c + 1;
                    };
                    myDatas.push([
                        column.owner.register_id,
                        column.owner.real_name,
                        column.owner.phone_number,
                        column.old_data.car_flag,
                        column.new_data.car_flag,
                        column.amount,
                        column.fee
                    ]);
                });
                myDatas.push(["升级用户总数：" + json.list.length, "升级财富车主总数：" + level_to_b, "升级豪金车主总数：" + level_to_c]);
            };
            return buffer = xlsx.build([{
                name: dateformat(new Date(), "yyyymmddHHMMss") + type,
                data: myDatas
            }]);
            // return buffer;
        } else {
            return buffer = xlsx.build([{
                name: "没有数据",
                data: [
                    ["没有数据 "]
                ]
            }]);
        };
        // myDatas.push(["总计收入：", json.totle_income, "总计支出：", json.totle_outgo, "拨出比例：", json.totle_outgo ? ((json.totle_income || 0) / json.totle_outgo * 100).toFixed(2) : 100 + "%"]);
        // var buffer = xlsx.build([{
        //     name: dateformat(new Date(), "yyyymmddHHMMss") + type,
        //     data: myDatas
        // }]);
        // return buffer;
        // fs.writeFileSync("/excel/报表" + dateformat(new Date(), "yyyymmddHHMMss") + ".xlsx", buffer, 'binary');
    })
}
module.exports = ExcelBuild;